package edu.yourl.presenter;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import edu.yourl.domain.Favoritable;
import edu.yourl.domain.User;
import edu.yourl.web.IHomePage;

public class HomePagePresenter extends BasePagePresenter implements IHomePagePresenter {

	@Transactional
	public void addGossip(IHomePage homePage) {
		User currentUser = getCurrentUser(homePage);
		currentUser.gossip(homePage.getMessageText());
		this.userRepository.add(currentUser);
		homePage.setGossipList(currentUser.getGossips());
	}

	@Override
	public List<Favoritable> getFavorites(IHomePage homePage) {
		User user = getCurrentUser(homePage);
		if (user != null) {
			return user.getFavorites();
		}
		return null;
	}

	@Override
	public void init(IHomePage homePage) {
		if (homePage.getCurrentUserNick() != null) {
			User user = this.userRepository.getUser(homePage.getCurrentUserNick());
			if (user != null) {
				homePage.setFavorites(user.getFavorites());
				homePage.setGossipList(user.getGossips());
			}
		}
	}

}
